package com.sajus.billing.client.util;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;



/**
 * Creates a proxy for the Manager interface.

 * @author 
 *
 * @param <T>
 */
public class ManagerProxy <T> implements InvocationHandler {
    private T foo;
    public static <T> T newInstance(T realFoo, Class inter) {
        return (T) Proxy.newProxyInstance(
        		ManagerProxy.class.getClassLoader(), 
        		new Class[]{ inter }, new ManagerProxy(realFoo));
    }

    private ManagerProxy(T realFoo) {
        this.foo = realFoo;
    }
 
    public Object invoke(Object proxy, Method method, Object[] args)
        throws Throwable {
    	DebugTimeCheckpoint cp = new DebugTimeCheckpoint(proxy.getClass().getName() );
        try {        	
            Object object = method.invoke(foo, args);
            return object;
        } catch (InvocationTargetException e) {
            throw e.getTargetException();
        } finally {
        	cp.done();
        }
    }
}
